//
//  BYCountTextField.m
//  OCDemo
//
//  Created by zhumuzhi on 2022/2/16.
//

#import "BYCountTextField.h"

@interface BYCountTextField ()

@end

@implementation BYCountTextField

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - SetData
- (void)setMaxCount:(NSInteger)maxCount {
    _maxCount = maxCount;
    [self setButtonEnabled:_count];
}

- (void)setMinCount:(NSInteger)minCount {
    _minCount = minCount;
    [self setButtonEnabled:_count];
}

- (void)setCount:(NSInteger)count {
    _count = count;
    [self setButtonEnabled:_count];
    self.textField.text = [NSString stringWithFormat:@"%ld",(long)count];
}

- (void)setEditCount:(NSInteger)editCount {
    _editCount = editCount;
}

- (void)setAddSubButtonEdit:(BOOL)addSubButtonEdit {
    if (addSubButtonEdit) {
        self.leftButton.enabled = YES;
        self.rightButton.enabled = YES;
    }
}

- (void)setButtonEnabled:(NSInteger)count {
    if (self.minCount >= self.maxCount) {
        if (count == self.minCount && count == self.maxCount) {
            self.leftButton.enabled = NO;
            self.rightButton.enabled = NO;
        } else {
            if (count < self.maxCount) {
                self.leftButton.enabled = NO;
                self.rightButton.enabled = YES;
            } else if (count > self.minCount) {
                self.leftButton.enabled = YES;
                self.rightButton.enabled = NO;
            } else {
                self.leftButton.enabled = NO;
                self.rightButton.enabled = NO;
            }
        }
    } else {
        if (count == self.minCount && count == self.maxCount) {
            self.leftButton.enabled = NO;
            self.rightButton.enabled = NO;
        } else {
            if (count <= self.minCount) {
                self.leftButton.enabled = NO;
                self.rightButton.enabled = YES;
            } else if (count >= self.maxCount) {
                self.leftButton.enabled = YES;
                self.rightButton.enabled = NO;
            } else {
                self.leftButton.enabled = YES;
                self.rightButton.enabled = YES;
            }
        }
    }
}

#pragma mark - Event
- (void)clickButton: (UIButton *)button {
    NSInteger count = self.textField.text.integerValue;
    if (button.tag == BYCountTextFieldChangeTypeAdd) {
        count = count + self.editCount;
    } else if (button.tag == BYCountTextFieldChangeTypeSub) {
        count = count - self.editCount;
    }
    [self setButtonEnabled:count];
    self.textField.text = [NSString stringWithFormat:@"%ld",(long)count];
    if (self.countBlock) {
        self.countBlock(self.textField.text.integerValue, button.tag);
    }
}

#pragma mark - NSNotificationCenter
- (void)textChange:(NSNotification *)noti {
    NSInteger count = self.textField.text.integerValue;
    [self setButtonEnabled:count];
    if (self.countBlock) {
        self.countBlock(count, BYCountTextFieldChangeTypeNone);
    }
}

#pragma mark - Init
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.editCount = 1;
        [self addSubview:self.leftButton];
        [self addSubview:self.textField];
        [self addSubview:self.rightButton];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(textChange:)
                                                     name:UITextFieldTextDidEndEditingNotification
                                                   object:self.textField];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.leftButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self);
        make.width.equalTo(@(self.height+5));
    }];
    
    [self.textField mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftButton.mas_right);
        make.right.equalTo(self.rightButton.mas_left);
        make.top.bottom.equalTo(self);
    }];
    
    [self.rightButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(self);
        make.width.equalTo(@(self.height+5));
    }];
    
    // 重新根据控件的Frame布局计算两个按钮的文字大小(比例计算为按钮宽高25时，字体为kFont(12))
    self.leftButton.titleLabel.font = MZFont(14 * self.height/25);
    self.rightButton.titleLabel.font = MZFont(14 * self.height/25);
    
    [super layoutSubviews];
}

#pragma mark - 去掉长按手势
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if ([UIMenuController sharedMenuController]) {
        [UIMenuController sharedMenuController].menuVisible = NO;
    }
    return NO;
}

#pragma mark - LazyGet

- (UIButton *)leftButton {
    if (!_leftButton) {
        _leftButton = [[UIButton alloc] init];
        _leftButton.tag = BYCountTextFieldChangeTypeSub;
        [_leftButton setTitle:@"-" forState:UIControlStateNormal];
        _leftButton.titleLabel.font = MZFont(14);
        [_leftButton setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
        [_leftButton setTitleColor:UIColorFromRGB(0xE4E3E4) forState:UIControlStateDisabled];
        [_leftButton setBackgroundColor:[UIColor clearColor] forState:UIControlStateNormal];
        [_leftButton setBackgroundColor:[UIColor clearColor] forState:UIControlStateHighlighted];
        [_leftButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftButton;
}

- (BYTextField *)textField {
    if (!_textField) {
        _textField = [[BYTextField alloc] init];
        _textField.keyboardType = UIKeyboardTypeNumberPad;
        _textField.font = [UIFont systemFontOfSize:kFont(12)];
        _textField.textColor = UIColorFromRGB(0x333333);
        _textField.textAlignment = NSTextAlignmentCenter;
        _textField.backgroundColor = [UIColor clearColor];
    }
    return _textField;
}

- (UIButton *)rightButton {
    if (!_rightButton) {
        _rightButton = [[UIButton alloc] init];
        _rightButton.tag = BYCountTextFieldChangeTypeAdd;
        [_rightButton setTitle:@"+" forState:UIControlStateNormal];
        _rightButton.titleLabel.font = MZFont(14);
        [_rightButton setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
        [_rightButton setTitleColor:UIColorFromRGB(0xE4E3E4) forState:UIControlStateDisabled];
        [_rightButton setBackgroundColor:[UIColor clearColor] forState:UIControlStateNormal];
        [_rightButton setBackgroundColor:[UIColor clearColor] forState:UIControlStateHighlighted];
        [_rightButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}


@end
